//
//  DevicesMainVC.swift
//  MIFI
//
//  Created by 金嗓子卡卡 on 2019/4/16.
//  Copyright © 2019 NJMifi. All rights reserved.
//

import UIKit
import MJRefresh
class DevicesMainVC: RootVC {

    private var datas : [DevicesModel.Info?] = []
    
    private lazy var tableView: UITableView = {
        let tab = UITableView(frame: CGRect(x: 0, y: naviView!.bottom, width: screenWidth, height: screenHeight - tabbarHeight - naviView!.bottom), style: .grouped)
        tab.backgroundColor = themeColor
        tab.rowHeight = tab.height / 5
        tab.register(DevicesListCell.self, forCellReuseIdentifier: "cell")
        tab.delegate = self
        tab.dataSource = self
        tab.separatorStyle = .none
        return tab
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func configView() {
        configNaviView(title: "Devices")
        leftBtn?.isHidden = true
        textLabel?.font = UIFont.systemFont(ofSize: 20)
        
        bottomLine?.isHidden = false
        bottomLine?.frame = CGRect(x: 12, y: naviView!.height - 1, width: naviView!.width - 12 * 2, height: 1)
        bottomLine?.backgroundColor = whiteColor
        
        view.addSubview(tableView)
        addMJRefreshHeader()
        
    }
    
    private func addMJRefreshHeader() {
        let tabHeader = MJRefreshNormalHeader {[weak self] in
            self!.getData()
        }
        tabHeader.stateLabel!.height = tabHeader.lastUpdatedTimeLabel!.bottom - tabHeader.stateLabel!.top
        tabHeader.stateLabel!.font = UIFont.boldSystemFont(ofSize: 16)
        tabHeader.lastUpdatedTimeLabel!.isHidden = true
        tabHeader.setTitle("Pull to refresh...", for: .idle)
        tabHeader.setTitle("Release to refresh...", for: .pulling)
        tabHeader.setTitle("Loading...", for: .refreshing)
        
        tableView.mj_header = tabHeader
    }
    
    private func getData() {
        net_getDevices { [weak self] data in
            if data != nil && data!.station_list!.count > 0 {
                self!.datas = data!.station_list!
            }else {
                self!.datas = []
            }
            self?.tableView.mj_header!.endRefreshing()
            self!.tableView.reloadData()
        }
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        getData()
    }
    
}

extension DevicesMainVC : UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! DevicesListCell
        cell.refreshData(data:datas[indexPath.row]!)
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
}


